﻿using System;
using Core.Authenticated;
using Core.Library;
using DataObject.Ref;
using DataObject;

namespace CMS.Controls.Config
{
    public partial class ConfigVote : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            ddlRound.SelectedValue = RefController.Ref_GetCurrentRound().ToString();
            if (RefController.Ref_GetLockVoteStatus()) ddlVote.SelectedValue = "1";
            else ddlVote.SelectedValue = "0";
            if (RefController.Ref_GetLockRegisterStatus()) ddlRegister.SelectedValue = "1";
            else ddlRegister.SelectedValue = "0";
            if (RefController.Ref_GetLockUploadStatus()) ddlUpload.SelectedValue = "1";
            else ddlUpload.SelectedValue = "0";
            if (RefController.Ref_GetLockEditStatus()) ddlUpdate.SelectedValue = "1";
            else ddlUpdate.SelectedValue = "0";
            if (RefController.Ref_GetMemberBreakCacheStatus()) ddlMemberBreak.SelectedValue = "1";
            else ddlMemberBreak.SelectedValue = "0";
            if (RefController.Ref_GetStartContestStatus()) ddlContest.SelectedValue = "1";
            else ddlContest.SelectedValue = "0";
            if (RefController.Ref_GetCheatVoteStatus()) ddlCheat.SelectedValue = "1";
            else ddlCheat.SelectedValue = "0";
            if (RefController.Ref_GetVoteDisplayStatus()) ddlVoteDisplay.SelectedValue = "1";
            else ddlVoteDisplay.SelectedValue = "0";
            if (RefController.Ref_GetTimelineDisplayStatus()) ddlTimelineDisplay.SelectedValue = "1";
            else ddlTimelineDisplay.SelectedValue = "0";
            if (RefController.Ref_GetLockCloneStatus()) ddlClone.SelectedValue = "1";
            else ddlClone.SelectedValue = "0";
        }

        protected void BtnSave(object sender, EventArgs e)
        {
            try
            {
                var round = ConvertUtility.ConvertTo<int>(ddlRound.SelectedValue);
                var vote = ConvertUtility.ConvertTo<int>(ddlVote.SelectedValue);
                var register = ConvertUtility.ConvertTo<int>(ddlRegister.SelectedValue);
                var update = ConvertUtility.ConvertTo<int>(ddlUpdate.SelectedValue);
                var upload = ConvertUtility.ConvertTo<int>(ddlUpload.SelectedValue);
                var memberBreak = ConvertUtility.ConvertTo<int>(ddlMemberBreak.SelectedValue);
                var contest = ConvertUtility.ConvertTo<int>(ddlContest.SelectedValue);
                var cheat = ConvertUtility.ConvertTo<int>(ddlCheat.SelectedValue);
                var votedisplay = ConvertUtility.ConvertTo<int>(ddlVoteDisplay.SelectedValue);
                var timeline = ConvertUtility.ConvertTo<int>(ddlTimelineDisplay.SelectedValue);
                var clone = ConvertUtility.ConvertTo<int>(ddlClone.SelectedValue);

                RefController.Ref_UpdateByKey(Constants.RefKey.CurrentRound, round);
                RefController.Ref_UpdateByKey(Constants.RefKey.LockVote, vote);
                RefController.Ref_UpdateByKey(Constants.RefKey.LockRegister, register);
                RefController.Ref_UpdateByKey(Constants.RefKey.LockEdit, update);
                RefController.Ref_UpdateByKey(Constants.RefKey.LockUpload, upload);
                RefController.Ref_UpdateByKey(Constants.RefKey.MemberBreakCache, memberBreak);
                RefController.Ref_UpdateByKey(Constants.RefKey.StartContest, contest);
                RefController.Ref_UpdateByKey(Constants.RefKey.CheatVote, cheat);
                RefController.Ref_UpdateByKey(Constants.RefKey.VoteDisplay, votedisplay);
                RefController.Ref_UpdateByKey(Constants.RefKey.TimelineDisplay, timeline);
                RefController.Ref_UpdateByKey(Constants.RefKey.LockClone, clone);
                ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Update thành công! Bạn hãy xóa cache config.", MessageType.Success);
            }
            catch (Exception ex)
            {

                ltMessage.Text = MessageUtility.ShowMessage("Lỗi", ex.Message, MessageType.Failure);
            }
        }
    }
}